package com.xx.springboot.improve.service.impl;

import com.xx.springboot.improve.domain.User;
import com.xx.springboot.improve.domain.UserRepository;
import com.xx.springboot.improve.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;

/**
 * author: XiongXin
 * description:
 * time: 2017/3/8.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public User getUserById(String id) {
        return userRepository.findOne(id);
    }

    @Override
    public Page<User> findAllUsersByPage(User user, Pageable pageable) {
        return userRepository.findAll(new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> namePath = root.get("name");
                criteriaQuery.where(criteriaBuilder.like(namePath, "%" + user.getName() + "%"));
                return null;
            }
        }, pageable);
    }
}
